package com.dabbo.starship.app.client;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.rmi.RemoteException;

import javax.swing.JFrame;

import com.dabbo.starship.client.listeners.LoseFocusListener;
import com.dabbo.starship.client.listeners.MouseClickListener;
import com.dabbo.starship.interfaces.Cell;
import com.dabbo.starship.interfaces.ConsoleMode;
import com.dabbo.starship.interfaces.WatchMouseClick;
import com.dabbo.starship.interfaces.WatchPopupClose;

public class GridViewUI extends JFrame implements WatchPopupClose, WatchMouseClick {
	
	private static final long serialVersionUID = 5125732071061735090L;

	private ConsoleUI displayPanel;
	private Cell currentCell;
	
	public GridViewUI(Cell currentCell) {
		this.currentCell = currentCell;
		
		displayPanel = new ConsoleUI(currentCell);
		displayPanel.setLayout(null);
		
		//this.addKeyListener(new KeyboardControlListener(this));
		
		getContentPane().add(displayPanel);

		this.setTitle("CONSOLE");
		Point po = displayPanel.getImageSize();
		this.setSize(po.x, po.y);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.addWindowFocusListener(new LoseFocusListener(this));
		this.addMouseListener(new MouseClickListener(this));

	}

	@Override
	public void closePopupWindow() {
		this.dispose();
	}

	@Override
	public void mouseClickTrigger(MouseEvent mouseEvent) throws RemoteException {
		System.out.println("Mouse click at x" + mouseEvent.getX() + ", y" + mouseEvent.getY());
		// Terminal Power Button
		if (mouseEvent.getX() >= 335 && mouseEvent.getX() <= 360) {
			if (mouseEvent.getY() >= 330 && mouseEvent.getY() <= 350) {
				if (currentCell.isActive()) {
					currentCell.deActivate();
				} else {
					currentCell.activate();
				}
				displayPanel.repaint();
			}
		}
		if (currentCell.isActive()) {
			if (currentCell.getCellType() == Cell.CELL_CONSOLE) {
				// Re-configure options
				switch (currentCell.getConsoleMode()) {
				case ConsoleMode.MODE_SELECT_MENU:
					if (currentCell.getConsoleMode() == ConsoleMode.MODE_SELECT_MENU) {
						if (mouseEvent.getX() >= 40 && mouseEvent.getX() <= 190) {
							if (mouseEvent.getY() >= 105 && mouseEvent.getY() <= 120) {
								currentCell.setConsoleMode(ConsoleMode.MODE_UNASSIGNED);
								System.out.println("Beep");
							}
						}
						if (mouseEvent.getX() >= 40 && mouseEvent.getX() <= 190) {
							if (mouseEvent.getY() >= 130 && mouseEvent.getY() <= 145) {
								currentCell.setConsoleMode(ConsoleMode.MODE_CAPTAIN);
							}
						}
						if (mouseEvent.getX() >= 210 && mouseEvent.getX() <= 360) {
							if (mouseEvent.getY() >= 105 && mouseEvent.getY() <= 120) {
								currentCell.setConsoleMode(ConsoleMode.MODE_CARGOBAY);
							}
						}
					}
					break;
				case ConsoleMode.MODE_CARGOBAY:
					if (mouseEvent.getX() >= 40 && mouseEvent.getX() <= 360) {
						if (mouseEvent.getY() >= 105 && mouseEvent.getY() <= 120) {
							Cell[][] roomCells = currentCell.getParent().getCells();
							for (int y = 0; y < roomCells.length; y++) {
								for (int x = 0; x < roomCells[y].length; x++) {
									if (roomCells[y][x].getCellType() == Cell.CELL_CARGO) {
										if (roomCells[y][x].isActive()) {
											roomCells[y][x].deActivate();
										} else {
											roomCells[y][x].activate();
										}
									}
								}
							}
						}
					}
					break;
				}
				// Reconfigure Button
				if (mouseEvent.getX() >= 260 && mouseEvent.getX() <= 360) {
					if (mouseEvent.getY() >= 290 && mouseEvent.getY() <= 305) {
						currentCell.setConsoleMode(ConsoleMode.MODE_SELECT_MENU);
					}
				}
			}
			
			// Lighting Toggle Button
			if (mouseEvent.getX() >= 40 && mouseEvent.getX() <= 160) {
				if (mouseEvent.getY() >= 290 && mouseEvent.getY() <= 305) {
					if (currentCell.getParent().isBlockLit()) {
						currentCell.getParent().setLighting(false);
					} else {
						currentCell.getParent().setLighting(true);
					}
					displayPanel.repaint();
				}
			}
		}
	}

}
